/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName AuthenticationManager.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.authmanager
{
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.socialize.Socialize;
	import com.gesturesoft.imultimax.socialize.events.AuthenticationEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.core.IUIComponent;
	import mx.events.CloseEvent;
	
	import spark.components.View;

	public class AuthenticationManager extends EventDispatcher
	{
		
		[Bindable]private var socialize:Socialize;
		
		public function AuthenticationManager()
		{
			//Constructor
		}
		
		public function processPrimaryAuthentication(view:View):void
		{
			socialize = new Socialize();
			socialize.getPrimaryAuthProcess(view);
			socialize.addEventListener(AuthenticationEvent.AUTHENTICATION_PRIMARY_SUCCESS,onAuthPrimarySuccess);
			socialize.addEventListener(AuthenticationEvent.AUTHENTICATION_PRIMARY_FAILED,onAuthPrimaryFail);
		}
		
		public function processSecondaryAuthentication(view:View):void
		{
			socialize = new Socialize();
			socialize.addEventListener(AuthenticationEvent.AUTHENTICATION_SUCCESS,onAuthSecondarySuccess);
			socialize.addEventListener(AuthenticationEvent.AUTHENTICATION_FAILED,onAuthSecondaryFail);
			socialize.addEventListener(AuthenticationEvent.AUTHENTICATION_NTWK_FAILED,onAuthSecondaryNtwkFail);
			socialize.getSocialAuth(getAuthMethod(),view);
		}
		
		private function onAuthPrimarySuccess(e:AuthenticationEvent):void
		{
			this.dispatchEvent(e.clone());
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_PRIMARY_SUCCESS,onAuthPrimarySuccess);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_PRIMARY_FAILED,onAuthPrimaryFail);
		}
		
		
		
		private function onAuthSecondarySuccess(e:AuthenticationEvent):void
		{
			this.dispatchEvent(e.clone());
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_SUCCESS,onAuthSecondarySuccess);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_FAILED,onAuthSecondaryFail);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_NTWK_FAILED,onAuthSecondaryNtwkFail);
		}
		
		private function onAuthPrimaryFail(e:AuthenticationEvent):void
		{
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_PRIMARY_SUCCESS,onAuthPrimarySuccess);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_PRIMARY_FAILED,onAuthPrimaryFail);
			this.dispatchEvent(e.clone());
			
		}
		
		private function onAuthSecondaryFail(e:AuthenticationEvent):void
		{
			
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_SUCCESS,onAuthSecondarySuccess);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_FAILED,onAuthSecondaryFail);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_NTWK_FAILED,onAuthSecondaryNtwkFail);
			this.dispatchEvent(e.clone());
			
		}
		
		private function onAuthSecondaryNtwkFail(e:AuthenticationEvent):void
		{
			
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_SUCCESS,onAuthSecondarySuccess);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_FAILED,onAuthSecondaryFail);
			socialize.removeEventListener(AuthenticationEvent.AUTHENTICATION_NTWK_FAILED,onAuthSecondaryNtwkFail);
			this.dispatchEvent(e.clone());
			
		}
		
		public function authSecondaryRequired(value:String):Boolean
		{
			var arcol:ArrayCollection = retrieveHandler("SELECT au_secondary FROM IM_AUTH_SETTINGS WHERE au_property='"+value+"'");
			var secondary:String = arcol[0].au_secondary.toString();
			return (secondary == 'true')?true:false;
		}
		
		public function authPrimaryRequired(value:String):Boolean
		{
			var arcol:ArrayCollection = retrieveHandler("SELECT au_primary FROM IM_AUTH_SETTINGS WHERE au_property='"+value+"'");
			var primary:String = arcol[0].au_primary.toString();
			return (primary == 'true')?true:false;
		}
		
		private function getAuthMethod():String
		{
			var arcol:ArrayCollection = retrieveHandler("SELECT usr_method FROM IM_USER");
			var method:String = arcol[0].usr_method.toString();
			return method;
		}
		
		private static function retrieveHandler(val:String):ArrayCollection
		{
			
			var result:ArrayCollection;
			
			var _iDBManager:IDatabaseManager = new DataBaseManager();
			try
			{
				
				result = ArrayCollection(_iDBManager.getData(val));
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("Application-retrieveHandler() : Database Read error");
				throw error;
				
			}
			
			return result;
			
		}
		
		public function processCancelClick():void
		{
			
			socialize.onCancelClick();
			
		}
		
	}
}